package tool;

import java.io.IOException;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import boot.Boot;
import function.Mp3File;

public class FileConnectionUtils {

	public static String[] getMp3sList(String dir) {

		int size = getNumMp3(dir);
		String[] mp3s = new String[size];

		FileConnection connection;
		try {
			connection = (FileConnection) Connector.open(dir);
			Enumeration mp3Enum = connection.list("*.mp3", true);

			for (int i = 0; i < mp3s.length; i++) {
				mp3s[i] = (String) mp3Enum.nextElement();
			}
			connection.close();
		} catch (IOException e) {
			Boot.alertException(e, new FileConnectionUtils(), "egtMp3sList()");
		}
		return mp3s;
	}

	public static int getNumMp3(String dir) {
		return getNum(dir, "*.mp3");
	}

	public static int getNum(String dir, String filter) {
		FileConnection connection;
		int index = 0;

		try {
			connection = (FileConnection) Connector.open(dir);
			Enumeration files = connection.list(filter, true);

			while (files.hasMoreElements()) {
				files.nextElement();
				index++;
			}
			connection.close();
		} catch (IOException e) {
			Boot.alertException(e, new FileConnectionUtils(), "getNum()");
		}
		return index;
	}

	public static void secureDelete(Mp3File mp3) {
		try {
			String title = mp3.getTitle();
			FileConnection connection = (FileConnection) Connector
					.open(Path.NOW + "/" + title + ".link");
			if ((connection != null) && (connection.exists())) {
				if (connection.canWrite()) {
					connection.rename(mp3.getTitle() + ".delete");
				} else {
					Boot.alertException(new IOException("Cannot rename link file."),
							new FileConnectionUtils(), "secureDelete()");
				}
			}
			connection.close();
		} catch (Throwable e) {
			Boot.alertException(e, new FileConnectionUtils(), "secureDelete()");
		}
	}

	public static String[] getNowMp3sList() {
		int size = getNum(Path.NOW, "*.link");
		String[] mp3s = new String[size];

		FileConnection connection;
		try {
			connection = (FileConnection) Connector.open(Path.NOW);
			Enumeration mp3Enum = connection.list("*.link", true);

			for (int i = 0; i < mp3s.length; i++) {
				String rawName = (String) mp3Enum.nextElement();
				mp3s[i] = rawName.substring(0, rawName.length() - 5) + ".mp3";
			}
			connection.close();
		} catch (IOException e) {
			Boot.alertException(e, new FileConnectionUtils(), "getNowMp3sList()");
		}
		return mp3s;
	}

	public static boolean isSDcardPresent() {
		boolean result = false;

		try {
			FileConnection connection = (FileConnection) Connector
					.open(FileUtils.SD_CARD);
			result = (connection != null);
		} catch (IOException e) {
			// Do nothing
		}
		return result;
	}

	public static int getLinkNumber() {
		return getNum(Path.NOW, "*.link");
	}

	public static String getFileAt(String dir, int wantedIndex, String filter) {
		FileConnection connection;
		int index = 0;
		String filename = null;

		try {
			connection = (FileConnection) Connector.open(dir);
			Enumeration enumFiles = connection.list(filter, true);

			while (enumFiles.hasMoreElements() && (index < wantedIndex)) {
				filename = (String) enumFiles.nextElement();
				index++;
			}
			connection.close();
		} catch (IOException e) {
			Boot.alertException(e, new FileConnectionUtils(), "getFileAt()");
		}

		if (index < wantedIndex) {
			Boot.alertException(new Exception("Cannot find file " + wantedIndex
					+ " in NOW."), new FileConnectionUtils(), "getFileAt()");
		}

		return filename;
	}
}
